package com.graduation.fq.teacher.process;

import com.alibaba.fastjson.JSONArray;
import com.graduation.fq.teacher.module.StudentInfo;
import com.graduation.fq.teacher.utils.Const;
import com.graduation.fq.teacher.utils.StudentListIndex;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jifang
 * Time 2016/5/28 11:14.
 */
public class StudentListProcess extends BaseProcess {
    private Map<StudentListIndex, String> urls = new HashMap<>();

    {
        urls.put(StudentListIndex.STUDENT_LIST, "/score/student_list.do?c_id=%s");
        urls.put(StudentListIndex.STUDENT_TOP_N, "/score/student_top_n.do?c_id=%s&n=%s");
        urls.put(StudentListIndex.STUDENT_UN_PASSED, "/score/student_un_passed.do?c_id=%s");
    }

    /**
     * request
     */
    private StudentListIndex index;
    private String cId;
    private Integer n;

    /**
     * response
     */
    private List<StudentInfo> students;

    public StudentListProcess(StudentListIndex index, String cId, Integer n) {
        this.index = index;
        this.cId = cId;
        this.n = n;
    }

    public StudentListIndex getIndex() {
        return index;
    }

    public void setIndex(StudentListIndex index) {
        this.index = index;
    }

    public String getcId() {
        return cId;
    }

    public void setcId(String cId) {
        this.cId = cId;
    }

    public Integer getN() {
        return n;
    }

    public void setN(Integer n) {
        this.n = n;
    }

    public List<StudentInfo> getStudents() {
        return students;
    }

    public void setStudents(List<StudentInfo> students) {
        this.students = students;
    }

    @Override
    String getRequestUrl() {
        if (getIndex() == StudentListIndex.STUDENT_TOP_N) {
            return String.format(urls.get(getIndex()), getcId(), getN());
        } else {
            return String.format(urls.get(getIndex()), getcId());
        }
    }

    @Override
    String getParameter() {
        return null;
    }

    @Override
    void onResult(JSONObject object) {
        int status = object.optInt("status");
        setStatus(status);
        if (status == Const.HTTP_SUCCESS) {
            String json = object.optJSONArray("data").toString();
            List<StudentInfo> students = JSONArray.parseArray(json, StudentInfo.class);
            setStudents(students);
        }
    }
}
